package com.papper.common.service.sys;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.papper.common.bean.sys.SysGroup;
import com.papper.common.mapper.sys.SysGroupMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * The type Sys group service.
 */
@Service("sysGroupService")
public class SysGroupService extends ServiceImpl<SysGroupMapper, SysGroup> {
	/**
	 * The Sys limit service.
	 */
	@Resource
	SysLimitService sysLimitService;

	/**
	 * 获取用户创建角色.
	 *
	 * @param systemId the system id
	 * @param userId   the user id
	 * @return the list
	 */
	public List<SysGroup> findByUserId(String systemId, String userId) {
		Example          example  = new Example(SysGroup.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("systemId", systemId);
		criteria.andEqualTo("userId", userId);
		example.setOrderByClause("up_id asc, group_sort asc");
		return find(example);
	}

	/**
	 * 获取组层级信息.
	 *
	 * @param family the family
	 * @param self   the self
	 * @return the list
	 */
	public List<SysGroup> findFamily(String family, boolean self) {
		Example          example  = new Example(SysGroup.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.orLike("upFamily", family + ",%");
		if (self) {
			criteria.orEqualTo("upFamily", family);
		}
		return find(example);
	}

	/**
	 * 匹配组下级信息.
	 *
	 * @param sysGroup the sys group
	 */
	public void matchFamily(SysGroup sysGroup) {
		SysGroup parent = findById(sysGroup.getUpId());
		String   family;
		if (parent != null) {
			family = parent.getUpFamily() + "," + sysGroup.getId();
		} else {
			family = "0," + sysGroup.getId();
		}
		// 同步数据
		SysGroup upd = new SysGroup();
		upd.setId(sysGroup.getId());
		upd.setUpFamily(family);
		update(upd);
		sysGroup.setUpFamily(family);
	}

	/**
	 * 删除系统组信息.
	 *
	 * @param systemId the system id
	 */
	public void deleteBySystem(Long systemId) {
		Example example = new Example(SysGroup.class);
		example.createCriteria().andEqualTo("systemId", systemId);
		List<SysGroup> lstTree  = find(example);
		Set<Long>      groupIds = Sets.newHashSet();
		Optional.ofNullable(lstTree).orElse(Lists.newArrayList()).forEach(group -> {
			groupIds.add(group.getId());
		});
		sysLimitService.delLimitedByGroupIds(Joiner.on(",").skipNulls().join(groupIds));
		delete(example);
	}

	/**
	 * 删除用户组信息.
	 *
	 * @param ids the ids
	 */
	public void deleteByUserIds(String ids) {
		Example          example  = new Example(SysGroup.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("userId", StringTool.stringToListString(ids, ","));
		List<SysGroup> lstTree  = find(example);
		Set<Long>      groupIds = Sets.newHashSet();
		Optional.ofNullable(lstTree).orElse(Lists.newArrayList()).forEach(group -> {
			groupIds.add(group.getId());
		});
		sysLimitService.delLimitedByGroupIds(Joiner.on(",").skipNulls().join(groupIds));
		delete(example);
	}
}